Euler Problem 151

A printing shop runs 16 batches (jobs) every week and each batch requires a sheet of special colour-proofing paper of size A5.

Every Monday morning, the foreman opens a new envelope, containing a large sheet of the special paper with size A1.

He proceeds to cut it in half, thus getting two sheets of size A2. Then he cuts one of them in half to get two sheets of size A3 and so on until he obtains the A5-size sheet needed for the first batch of the week.

All the unused sheets are placed back in the envelope.

At the beginning of each subsequent batch, he takes from the envelope one sheet of paper at random. If it is of size A5, he uses it. If it is larger, he repeats the 'cut-in-half' procedure until he has what he needs and any remaining sheets are always placed back in the envelope.

Excluding the first and last batch of the week, find the expected number of times (during each week) that the foreman finds a single sheet of paper in the envelope.

Give your answer rounded to six decimal places using the format x.xxxxxx .


In [1]:
remember = {}

def cut_paper(v = [0,0,0,0,1], n = 16):
    if n == 1:
        return 1
    strv = str(v)
    if strv in remember:
        return remember[strv]
    sv = sum(v)
    if sv == 1:
        answer = 1
    else:
        answer = 0
    for i in range(len(v)):
        if v[i]:
            v1 = v[:]
            for j in range(i):
                v1[j] += 1
            v1[i] -= 1
            answer += 1.0*v[i]/sv * cut_paper(v1, n-1)
    remember[strv] = answer
    return answer

print ("%.6f" % (cut_paper() - 2))


0.464399

Note: Old code, should be updated.